using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using ChatLib;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.IO;
using System.Threading;
using System.Net;

namespace Chat
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	///
	public class Form1 : System.Windows.Forms.Form
	{

		#region Designer code
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.MenuItem menuSendMessage;
		private System.Windows.Forms.MenuItem menuAddContact;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem10;
		private System.Windows.Forms.MenuItem menuItem13;
		private System.Windows.Forms.Timer tmrMessageReceive;
		private System.Windows.Forms.Panel pnlContacts;
		private System.Windows.Forms.MenuItem menuChangeUser;
		private System.Windows.Forms.MenuItem menuSignOut;
		private System.Windows.Forms.MenuItem menuMin;
		private System.Windows.Forms.MenuItem menuExit;
		private System.Windows.Forms.Timer tmrContactUpdate;
		private System.Windows.Forms.MenuItem menuRemoveContact;
		private System.Windows.Forms.MenuItem menuNetworkSettings;
		private System.Windows.Forms.StatusBar statusBar;

		
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem conMenuSendMessage;
		private System.Windows.Forms.MenuItem conMenuAddContact;
		private System.Windows.Forms.ContextMenu conMenu;
		private System.Windows.Forms.ContextMenu conMenuContactsPanel;
		private System.Windows.Forms.MenuItem conMenuRefreshContactsPanel;
		private System.Windows.Forms.MenuItem conMenuRemoveContact;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem conMenuPanelRemoveContact;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.NotifyIcon notifyIcon;
		private System.Windows.Forms.MenuItem menuChat;
		private System.Windows.Forms.MenuItem menuAbout;
		private System.Windows.Forms.ContextMenu notifyMenu;
		private System.Windows.Forms.MenuItem notMenuSend;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.MenuItem menuItem15;
		private System.Windows.Forms.MenuItem notMenuSignOut;
		private System.Windows.Forms.MenuItem notMenuSignIn;
		private System.Windows.Forms.MenuItem notMenuMin;
		private System.Windows.Forms.MenuItem notMenuExit;
		private System.Windows.Forms.MenuItem notMenuAbout;
		private System.Windows.Forms.MenuItem conMenuPanelAddContact;
		private MenuItem menuItem1;
		private MenuItem menuItem2;
		private MenuItem menuScreencast;
		private MenuItem menuShowScreencasts;
		private MenuItem menuItem3;
		private MenuItem menuItem9;
		private MenuItem menuTasks;
		private MenuItem menuItem11;
		#endregion
		HttpChannel channel;
		string hostIP;
		FormCreateScreencast frmCreateScreencast = new FormCreateScreencast();
		


		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//				
			channel = new HttpChannel();
			ChannelServices.RegisterChannel(channel);			
			
			statusBar.Text="Loading Network Settings...";
			if(File.Exists("NetSet.Dat"))
			{
				FileStream fs = new FileStream("NetSet.Dat",FileMode.Open);
				BinaryReader br = new BinaryReader(fs);
				hostIP = br.ReadString();
				fs.Close();
				br.Close();
			}
			else
			{
				IPHostEntry host;
				string localIP = "";
				host = Dns.GetHostEntry(Dns.GetHostName());
				foreach (IPAddress ip in host.AddressList)
				{
					if (ip.AddressFamily.ToString() == "InterNetwork")
					{
						localIP = ip.ToString();
					}
				}
				hostIP = localIP;
			}
			statusBar.Text="Network Settings loaded.";
			try
			{
				MarshalByRefObject obj = (MarshalByRefObject)RemotingServices.Connect(typeof(IServer),"http://"+hostIP+":8080/Server");
				Global.server=obj as IServer;
				(obj as RemotingClientProxy).Timeout=5000;
			}
			catch
			{				
				return;
			}
		}

		internal Global Global
		{
			get
			{
				throw new System.NotImplementedException();
			}
			set
			{
			}
		}
	
		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.Show();
			SignIn();
			//this.Show();
		}
		private void Contact_Click(object sender, System.EventArgs e)
		{
			OpenFormMessage(((ChatControls.ChatMasalaContact)sender).Contact);
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
			this.mainMenu = new System.Windows.Forms.MainMenu(this.components);
			this.menuChat = new System.Windows.Forms.MenuItem();
			this.menuSendMessage = new System.Windows.Forms.MenuItem();
			this.menuAddContact = new System.Windows.Forms.MenuItem();
			this.menuRemoveContact = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuChangeUser = new System.Windows.Forms.MenuItem();
			this.menuSignOut = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuScreencast = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.menuShowScreencasts = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuTasks = new System.Windows.Forms.MenuItem();
			this.menuItem11 = new System.Windows.Forms.MenuItem();
			this.menuMin = new System.Windows.Forms.MenuItem();
			this.menuExit = new System.Windows.Forms.MenuItem();
			this.menuItem10 = new System.Windows.Forms.MenuItem();
			this.menuNetworkSettings = new System.Windows.Forms.MenuItem();
			this.menuItem13 = new System.Windows.Forms.MenuItem();
			this.menuAbout = new System.Windows.Forms.MenuItem();
			this.tmrMessageReceive = new System.Windows.Forms.Timer(this.components);
			this.pnlContacts = new System.Windows.Forms.Panel();
			this.tmrContactUpdate = new System.Windows.Forms.Timer(this.components);
			this.statusBar = new System.Windows.Forms.StatusBar();
			this.conMenu = new System.Windows.Forms.ContextMenu();
			this.conMenuSendMessage = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.conMenuAddContact = new System.Windows.Forms.MenuItem();
			this.conMenuRemoveContact = new System.Windows.Forms.MenuItem();
			this.conMenuContactsPanel = new System.Windows.Forms.ContextMenu();
			this.conMenuPanelAddContact = new System.Windows.Forms.MenuItem();
			this.conMenuPanelRemoveContact = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.conMenuRefreshContactsPanel = new System.Windows.Forms.MenuItem();
			this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
			this.notifyMenu = new System.Windows.Forms.ContextMenu();
			this.notMenuSend = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.notMenuSignIn = new System.Windows.Forms.MenuItem();
			this.notMenuSignOut = new System.Windows.Forms.MenuItem();
			this.menuItem15 = new System.Windows.Forms.MenuItem();
			this.notMenuAbout = new System.Windows.Forms.MenuItem();
			this.notMenuMin = new System.Windows.Forms.MenuItem();
			this.notMenuExit = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
			this.menuChat,
			this.menuItem10,
			this.menuItem13});
			// 
			// menuChat
			// 
			this.menuChat.Index = 0;
			this.menuChat.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
			this.menuSendMessage,
			this.menuAddContact,
			this.menuRemoveContact,
			this.menuItem4,
			this.menuChangeUser,
			this.menuSignOut,
			this.menuItem2,
			this.menuScreencast,
			this.menuShowScreencasts,
			this.menuItem7,
			this.menuTasks,
			this.menuItem11,
			this.menuMin,
			this.menuExit});
			this.menuChat.Text = "Chat";
			// 
			// menuSendMessage
			// 
			this.menuSendMessage.Enabled = false;
			this.menuSendMessage.Index = 0;
			this.menuSendMessage.Text = "Send an Instant Message..";
			this.menuSendMessage.Click += new System.EventHandler(this.menuSendMessage_Click);
			// 
			// menuAddContact
			// 
			this.menuAddContact.Enabled = false;
			this.menuAddContact.Index = 1;
			this.menuAddContact.Text = "Add a Contact...";
			this.menuAddContact.Click += new System.EventHandler(this.menuAddContact_Click);
			// 
			// menuRemoveContact
			// 
			this.menuRemoveContact.Enabled = false;
			this.menuRemoveContact.Index = 2;
			this.menuRemoveContact.Text = "Remove a Contact...";
			this.menuRemoveContact.Click += new System.EventHandler(this.menuRemoveContact_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 3;
			this.menuItem4.Text = "-";
			// 
			// menuChangeUser
			// 
			this.menuChangeUser.Index = 4;
			this.menuChangeUser.Text = "Sign In";
			this.menuChangeUser.Click += new System.EventHandler(this.menuChangeUser_Click);
			// 
			// menuSignOut
			// 
			this.menuSignOut.Enabled = false;
			this.menuSignOut.Index = 5;
			this.menuSignOut.Text = "Sign Out";
			this.menuSignOut.Click += new System.EventHandler(this.menuSignOut_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 6;
			this.menuItem2.Text = "-";
			// 
			// menuScreencast
			// 
			this.menuScreencast.Index = 7;
			this.menuScreencast.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
			this.menuItem3,
			this.menuItem9});
			this.menuScreencast.Text = "Control Screencast";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.Text = "Start...";
			this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 1;
			this.menuItem9.Text = "Stop";
			this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
			// 
			// menuShowScreencasts
			// 
			this.menuShowScreencasts.Index = 8;
			this.menuShowScreencasts.Text = "Connect to screencast...";
			this.menuShowScreencasts.Click += new System.EventHandler(this.menuShowScreencasts_Click);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 9;
			this.menuItem7.Text = "-";
			// 
			// menuTasks
			// 
			this.menuTasks.Index = 10;
			this.menuTasks.Text = "Tasks";
			this.menuTasks.Click += new System.EventHandler(this.menuTasks_Click);
			// 
			// menuItem11
			// 
			this.menuItem11.Index = 11;
			this.menuItem11.Text = "-";
			// 
			// menuMin
			// 
			this.menuMin.Index = 12;
			this.menuMin.Text = "Minimize to System Tray";
			// 
			// menuExit
			// 
			this.menuExit.Index = 13;
			this.menuExit.Text = "Exit";
			this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
			// 
			// menuItem10
			// 
			this.menuItem10.Index = 1;
			this.menuItem10.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
			this.menuNetworkSettings});
			this.menuItem10.Text = "Settings";
			// 
			// menuNetworkSettings
			// 
			this.menuNetworkSettings.Index = 0;
			this.menuNetworkSettings.Text = "Network Settings";
			this.menuNetworkSettings.Click += new System.EventHandler(this.menuNetworkSettings_Click);
			// 
			// menuItem13
			// 
			this.menuItem13.Index = 2;
			this.menuItem13.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
			this.menuAbout});
			this.menuItem13.Text = "Help";
			// 
			// menuAbout
			// 
			this.menuAbout.Index = 0;
			this.menuAbout.Text = "About..";
			this.menuAbout.Click += new System.EventHandler(this.menuAbout_Click);
			// 
			// tmrMessageReceive
			// 
			this.tmrMessageReceive.Interval = 1000;
			this.tmrMessageReceive.Tick += new System.EventHandler(this.tmrMessageReceive_Tick);
			// 
			// pnlContacts
			// 
			this.pnlContacts.AutoScroll = true;
			this.pnlContacts.BackColor = System.Drawing.Color.White;
			this.pnlContacts.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pnlContacts.Location = new System.Drawing.Point(8, 8);
			this.pnlContacts.Name = "pnlContacts";
			this.pnlContacts.Size = new System.Drawing.Size(208, 240);
			this.pnlContacts.TabIndex = 0;
			this.pnlContacts.Resize += new System.EventHandler(this.pnlContacts_Resize);
			// 
			// tmrContactUpdate
			// 
			this.tmrContactUpdate.Interval = 3000;
			this.tmrContactUpdate.Tick += new System.EventHandler(this.tmrContactUpdate_Tick);
			// 
			// statusBar
			// 
			this.statusBar.Location = new System.Drawing.Point(0, 262);
			this.statusBar.Name = "statusBar";
			this.statusBar.Size = new System.Drawing.Size(230, 22);
			this.statusBar.TabIndex = 1;
			// 
			// conMenu
			// 
			this.conMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
			this.conMenuSendMessage,
			this.menuItem1,
			this.menuItem5,
			this.conMenuAddContact,
			this.conMenuRemoveContact});
			// 
			// conMenuSendMessage
			// 
			this.conMenuSendMessage.Index = 0;
			this.conMenuSendMessage.Text = "Send an Instant Message...";
			this.conMenuSendMessage.Click += new System.EventHandler(this.conMenuSendMessage_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 1;
			this.menuItem1.Text = "Send Task...";
			this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 2;
			this.menuItem5.Text = "-";
			// 
			// conMenuAddContact
			// 
			this.conMenuAddContact.Index = 3;
			this.conMenuAddContact.Text = "Add a Contact...";
			this.conMenuAddContact.Click += new System.EventHandler(this.menuAddContact_Click);
			// 
			// conMenuRemoveContact
			// 
			this.conMenuRemoveContact.Index = 4;
			this.conMenuRemoveContact.Text = "Remove this Contact...";
			this.conMenuRemoveContact.Click += new System.EventHandler(this.conMenuRemoveContact_Click);
			// 
			// conMenuContactsPanel
			// 
			this.conMenuContactsPanel.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
			this.conMenuPanelAddContact,
			this.conMenuPanelRemoveContact,
			this.menuItem6,
			this.conMenuRefreshContactsPanel});
			// 
			// conMenuPanelAddContact
			// 
			this.conMenuPanelAddContact.Index = 0;
			this.conMenuPanelAddContact.Text = "Add a Contact...";
			this.conMenuPanelAddContact.Click += new System.EventHandler(this.menuAddContact_Click);
			// 
			// conMenuPanelRemoveContact
			// 
			this.conMenuPanelRemoveContact.Index = 1;
			this.conMenuPanelRemoveContact.Text = "Remove a Contact...";
			this.conMenuPanelRemoveContact.Click += new System.EventHandler(this.menuRemoveContact_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 2;
			this.menuItem6.Text = "-";
			// 
			// conMenuRefreshContactsPanel
			// 
			this.conMenuRefreshContactsPanel.Index = 3;
			this.conMenuRefreshContactsPanel.Text = "Refresh Contact Panel";
			this.conMenuRefreshContactsPanel.Click += new System.EventHandler(this.conMenuRefreshContactsPanel_Click);
			// 
			// notifyIcon
			// 
			this.notifyIcon.ContextMenu = this.notifyMenu;
			this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon")));
			this.notifyIcon.Text = "Chat Masala";
			this.notifyIcon.Visible = true;
			// 
			// notifyMenu
			// 
			this.notifyMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
			this.notMenuSend,
			this.menuItem8,
			this.notMenuSignIn,
			this.notMenuSignOut,
			this.menuItem15,
			this.notMenuAbout,
			this.notMenuMin,
			this.notMenuExit});
			// 
			// notMenuSend
			// 
			this.notMenuSend.Enabled = false;
			this.notMenuSend.Index = 0;
			this.notMenuSend.Text = "Send an Instant Message...";
			this.notMenuSend.Click += new System.EventHandler(this.menuSendMessage_Click);
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 1;
			this.menuItem8.Text = "-";
			// 
			// notMenuSignIn
			// 
			this.notMenuSignIn.Index = 2;
			this.notMenuSignIn.Text = "Sign In";
			this.notMenuSignIn.Click += new System.EventHandler(this.menuChangeUser_Click);
			// 
			// notMenuSignOut
			// 
			this.notMenuSignOut.Enabled = false;
			this.notMenuSignOut.Index = 3;
			this.notMenuSignOut.Text = "Sign Out";
			this.notMenuSignOut.Click += new System.EventHandler(this.menuSignOut_Click);
			// 
			// menuItem15
			// 
			this.menuItem15.Index = 4;
			this.menuItem15.Text = "-";
			// 
			// notMenuAbout
			// 
			this.notMenuAbout.Index = 5;
			this.notMenuAbout.Text = "About Chat Masala";
			this.notMenuAbout.Click += new System.EventHandler(this.menuAbout_Click);
			// 
			// notMenuMin
			// 
			this.notMenuMin.Index = 6;
			this.notMenuMin.Text = "Minimize";
			// 
			// notMenuExit
			// 
			this.notMenuExit.Index = 7;
			this.notMenuExit.Text = "Exit";
			this.notMenuExit.Click += new System.EventHandler(this.menuExit_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(230, 284);
			this.Controls.Add(this.statusBar);
			this.Controls.Add(this.pnlContacts);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu;
			this.MinimumSize = new System.Drawing.Size(232, 314);
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Chat";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.Resize += new System.EventHandler(this.Form1_Resize);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			using(Form1 frm1 = new Form1())
			{
				Application.Run(frm1);
			}
		}
		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			notifyIcon.Dispose();
			ChannelServices.UnregisterChannel(channel);
			try
			{
				frmCreateScreencast.StopServer();
				Global.server.SignOut(Global.username);
			}
			finally
			{
				Application.Exit();
			}
		}
		private void menuSendMessage_Click(object sender, System.EventArgs e)
		{
			using(FormSelectContact frmContact = new FormSelectContact())
			{
				if(frmContact.ShowDialog(this)==DialogResult.OK)
				{
					if(frmContact.txtContact.Text==Global.username)
					{
						MessageBox.Show("You can not send a message to yourself.");
						return;
					}
					if(Global.windowList.Contains(frmContact.txtContact.Text))
					{
						((FormMessage)Global.windowList[frmContact.txtContact.Text]).Focus();
					}
					else
					{
						FormMessage frmMessage = new FormMessage();
						frmMessage.contact=frmContact.txtContact.Text;
						frmMessage.Text=frmContact.txtContact.Text+" - Instant Message";
						Global.windowList.Add(frmContact.txtContact.Text,frmMessage);				
						frmMessage.Show();
					}
				}
			}
		}
		private void tmrMessageReceive_Tick(object sender, System.EventArgs e)
		{
			LetterReceive letter;
			try
			{
				letter = Global.server.Receive(Global.username);
			}
			catch
			{
				AbortConnection();
				return;
			}
			if(letter.From=="")
			{
				return;
			}
			if(Global.windowList.Contains(letter.From))
			{	
				if(letter.Message=="BUZZ BHI KARO")
				{
					((FormMessage)Global.windowList[letter.From]).Focus();			
				}
				((FormMessage)Global.windowList[letter.From]).AddText(letter.From,letter.Message);

			}
			else
			{
				FormMessage frmMessage = new FormMessage();
				frmMessage.contact=letter.From;
				frmMessage.Text=letter.From+" - Instant Message";
				frmMessage.AddText(letter.From,letter.Message);
				Global.windowList.Add(letter.From,frmMessage);
				frmMessage.Show();
			}
		}
		private void menuAddContact_Click(object sender, System.EventArgs e)
		{
			FormAddContact frmAddContact = new FormAddContact();
			if(frmAddContact.ShowDialog()==DialogResult.OK)
			{
				UpdatePanelContact();
			}
		}
		private void menuSignOut_Click(object sender, System.EventArgs e)
		{
			SignOut();
		}
		private void tmrContactUpdate_Tick(object sender, System.EventArgs e)
		{
			try
			{
				foreach(object o in Global.contactList)
				{
					((ChatControls.ChatMasalaContact)o).Online=Global.server.IsVisible((o as ChatControls.ChatMasalaContact).Contact);
				}
				pnlContacts.Update();
			}
			catch
			{
				AbortConnection();
				return;
			}
		}
		private void menuRemoveContact_Click(object sender, System.EventArgs e)
		{
			FormSelectContact frmSelectContact = new FormSelectContact();
			if(frmSelectContact.ShowDialog()==DialogResult.OK)
			{
				try
				{
					Global.server.RemoveContact(Global.username,frmSelectContact.txtContact.Text);
					UpdatePanelContact();
				}
				catch
				{
					AbortConnection();
					return;
				}
			}
		}
		private void Form1_Resize(object sender, System.EventArgs e)
		{
			pnlContacts.Width=this.Width-24;
			pnlContacts.Height=this.Height-88;
		}
		private void pnlContacts_Resize(object sender, System.EventArgs e)
		{
			foreach(object o in Global.contactList)
			{
				((ChatControls.ChatMasalaContact)o).Width=pnlContacts.Width-32;
			}
		}
		private void menuChangeUser_Click(object sender, System.EventArgs e)
		{
			SignIn();
		}
		private void menuExit_Click(object sender, System.EventArgs e)
		{
			try
			{
				frmCreateScreencast.StopServer();
				Global.server.SignOut(Global.username);
			}
			finally
			{
				Application.Exit();
			}			
		}
		private void menuNetworkSettings_Click(object sender, System.EventArgs e)
		{
			FormNetworkSettings frmNetworkSettings = new FormNetworkSettings();
			frmNetworkSettings.txtIP.Text=hostIP;
			if(frmNetworkSettings.ShowDialog()==DialogResult.OK)
			{
				hostIP=frmNetworkSettings.txtIP.Text;
				try
				{
					AbortConnection();
					MarshalByRefObject obj = (MarshalByRefObject)RemotingServices.Connect(typeof(IServer),"http://"+hostIP+":8080/Server");
					Global.server=obj as IServer;
				}
				catch
				{
					AbortConnection();
					return;
				}
			}
		}
		private void conMenuSendMessage_Click(object sender, System.EventArgs e)
		{
			OpenFormMessage((conMenu.SourceControl as ChatControls.ChatMasalaContact).Contact);
		}
		private void UpdatePanelContact()
		{
			statusBar.Text="Requesting server for contact...";
			ArrayList contacts=null;
			try
			{
				contacts = Global.server.GetContacts(Global.username);	
			}
			catch
			{
				AbortConnection();
				return;
			}
			statusBar.Text="Contacts downloaded";

			
			Label lblInfo = new Label();
			lblInfo.Text="Contacts :";
			lblInfo.Location=new System.Drawing.Point(8,4);
			lblInfo.Size=new System.Drawing.Size(100,16);
			
			pnlContacts.Controls.Clear();
			Global.contactList.Clear();
			pnlContacts.Controls.Add(lblInfo);
			int i = 20;
			statusBar.Text="Updating Contact panel...";
			foreach(object o in contacts)
			{						
				ChatControls.ChatMasalaContact temp = new ChatControls.ChatMasalaContact();
				temp.Contact=o as string;
				statusBar.Text="Adding contact "+o as string;
				try
				{
					temp.Online=Global.server.IsVisible(o as String);
				}
				catch
				{
					AbortConnection();
					return;
				}
				temp.Location=new System.Drawing.Point(8,i);
				temp.Size=new System.Drawing.Size(pnlContacts.Width-32,16);
				temp.DoubleClick+=new System.EventHandler(this.Contact_Click);
				temp.ContextMenu=conMenu;

				pnlContacts.Controls.Add(temp);
				Global.contactList.Add(temp);
				i +=18;
			}
			statusBar.Text="Contacts updated.";
		}
		private void OpenFormMessage(string contact)
		{
			if(Global.windowList.Contains(contact))
			{
				((FormMessage)Global.windowList[contact]).Focus();
			}
			else
			{
				FormMessage frmMessage = new FormMessage();
				frmMessage.contact=contact;
				frmMessage.Text=contact+" - Instant Message";
				frmMessage.Show();
				Global.windowList.Add(contact,frmMessage);					
			}
		}
		private void SignOut()
		{
			ArrayList a = new ArrayList();
			foreach(string key in Global.windowList.Keys)
				a.Add(key);
			foreach(string key in a)
				(Global.windowList[key] as FormMessage).Close();

			this.menuAddContact.Enabled=false;
			this.menuSendMessage.Enabled=false;
			this.menuRemoveContact.Enabled=false;
			this.menuChangeUser.Text="Sign In...";
			this.menuSignOut.Enabled=false;
			this.pnlContacts.ContextMenu=null;

			this.notMenuSend.Enabled=false;
			this.notMenuSignIn.Text="Sign In...";
			this.notMenuSignOut.Enabled=false;

			tmrMessageReceive.Enabled=false;
			tmrContactUpdate.Enabled=false;
			try
			{
				Global.server.SignOut(Global.username);
				statusBar.Text="Signed Out.";
			}
			catch
			{
				AbortConnection();
				return;
			}
			finally
			{
				pnlContacts.Controls.Clear();
				Global.contactList.Clear();
				Global.username="";
				
			}
		}
		private void conMenuRemoveContact_Click(object sender, System.EventArgs e)
		{
			try
			{
				Global.server.RemoveContact(Global.username,(conMenu.SourceControl as ChatControls.ChatMasalaContact).Contact);
				UpdatePanelContact();
			}
			catch
			{
				AbortConnection();
				return;
			}
		}
		private void conMenuRefreshContactsPanel_Click(object sender, System.EventArgs e)
		{
			this.UpdatePanelContact();
		}
		private void SignIn()
		{
			
			notifyIcon.ContextMenu=null;
			statusBar.Text="Sign In";
			FormSignIn frmSignIn = new FormSignIn();
			switch(frmSignIn.ShowDialog(this))
			{
				case DialogResult.OK:
					SignOut();
					Global.username=frmSignIn.txtUsername.Text;
					try
					{
						ArrayList offline = Global.server.ReceiveOffline(Global.username);
						if(offline.Count>0)
						{
							FormOfflineMessages frmOffline = new FormOfflineMessages(offline);
							frmOffline.Show();
						}						
					}
					catch
					{
						AbortConnection();
						return;
					}
					statusBar.Text="Signed In";

					tmrMessageReceive.Enabled=true;
					tmrContactUpdate.Enabled=true;
		
					this.menuAddContact.Enabled=true;
					this.menuSendMessage.Enabled=true;
					this.menuRemoveContact.Enabled=true;
					this.menuChangeUser.Text="Sign In as different user...";
					this.menuSignOut.Enabled=true;
					this.pnlContacts.ContextMenu=this.conMenuContactsPanel;

					this.notMenuSend.Enabled=true;
					this.notMenuSignIn.Text="Sign In as different user...";
					this.notMenuSignOut.Enabled=true;

					this.UpdatePanelContact();
					break;
			}
			notifyIcon.ContextMenu=this.notifyMenu;			
		}
		private void AbortConnection()
		{
			tmrMessageReceive.Enabled=false;
			tmrContactUpdate.Enabled=false;

			this.menuAddContact.Enabled=false;
			this.menuSendMessage.Enabled=false;
			this.menuRemoveContact.Enabled=false;
			this.menuChangeUser.Text="Sign In...";
			this.menuSignOut.Enabled=false;
			this.pnlContacts.ContextMenu=null;

			this.notMenuSend.Enabled=false;
			this.notMenuSignIn.Text="Sign In...";
			this.notMenuSignOut.Enabled=false;

			ArrayList a = new ArrayList();
			foreach(string key in Global.windowList.Keys)
				a.Add(key);
			foreach(string key in a)
				(Global.windowList[key] as FormMessage).Close();
			Global.username="";
			pnlContacts.Controls.Clear();
			Global.contactList.Clear();
			statusBar.Text="Connection with server failed.";
			this.Focus();
			MessageBox.Show("Connection with server failed. Please check your network settings");
		}
		private void menuAbout_Click(object sender, System.EventArgs e)
		{
			FormAbout frmAbout = new FormAbout();
			frmAbout.ShowDialog();
		}
		private void menuItem3_Click(object sender, EventArgs e)
		{
			try
			{
				frmCreateScreencast.Show();
			}
			catch 
			{
				FormCreateScreencast frmCreateScreencast = new FormCreateScreencast();
				frmCreateScreencast.Show();
			}
		}
		private void menuItem9_Click(object sender, EventArgs e)
		{
			try
			{
				frmCreateScreencast.StopServer();
			}
			catch 
			{
				FormCreateScreencast frmCreateScreencast = new FormCreateScreencast();
				frmCreateScreencast.StopServer();
			}
		}
		private void menuShowScreencasts_Click(object sender, EventArgs e)
		{
			FormConnectScreencast frmScreencasts = new FormConnectScreencast();
			frmScreencasts.Show();
		}
		private void menuTasks_Click(object sender, EventArgs e)
		{
			FormTasks frmTasks = new FormTasks();
			frmTasks.Show();
		}

		private void menuItem1_Click(object sender, EventArgs e)
		{
			string contact = (conMenu.SourceControl as ChatControls.ChatMasalaContact).Contact.ToString();
			FormCreateTask frmCreateTask = new FormCreateTask(contact);
			frmCreateTask.Show();
		}
	}
	class Global
	{
		internal static IServer server;
		internal static Hashtable windowList;
		internal static ArrayList contactList;
		internal static string username;

		static Global()
		{
			windowList = new Hashtable();
			contactList = new ArrayList();
			username="";
		}

		public ChatLib.IServer IServer
		{
			get
			{
				throw new System.NotImplementedException();
			}
			set
			{
			}
		}
	}
}